package com.huawei.hms.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Looper;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hms.adapter.a.a;
import com.huawei.hms.adapter.a.c;
import com.huawei.hms.adapter.b.b;
import com.huawei.hms.adapter.internal.CommonCode.MapKey;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Checker;
import com.huawei.hms.utils.PackageManagerHelper;
import com.huawei.hms.utils.PackageManagerHelper.PackageStates;
import java.util.concurrent.atomic.AtomicBoolean;

public class AvailableAdapter {
    private static AtomicBoolean c = new AtomicBoolean(false);
    private static int d = 0;
    private final int a;
    private AvailableCallBack b;
    private c e = new c(this) {
        final /* synthetic */ AvailableAdapter a;

        {
            this.a = r1;
        }

        public void a(int i) {
            AvailableCallBack a = this.a.c();
            if (a == null) {
                HMSLog.e("AvailableAdapter", "onUpdateResult baseCallBack null");
                if (this.a.e != null) {
                    a.b().b(this.a.e);
                    this.a.e = null;
                    return;
                }
                return;
            }
            a.onComplete(i);
            if (this.a.e != null) {
                a.b().b(this.a.e);
                this.a.e = null;
            }
            AvailableAdapter.d = i;
            HMSLog.i("AvailableAdapter", "user response " + AvailableAdapter.d);
            AvailableAdapter.c.set(false);
        }

        public void a(Intent intent, String str) {
        }
    };

    public interface AvailableCallBack {
        void onComplete(int i);
    }

    public AvailableAdapter(int i) {
        this.a = i;
    }

    public int isHuaweiMobileServicesAvailable(Context context) {
        Checker.checkNonNull(context, "context must not be null.");
        if (VERSION.SDK_INT < 16) {
            return 21;
        }
        PackageManagerHelper packageManagerHelper = new PackageManagerHelper(context);
        com.huawei.hms.utils.a a = com.huawei.hms.utils.a.a(context);
        PackageStates a2 = a.a();
        if (PackageStates.NOT_INSTALLED.equals(a2)) {
            return 1;
        }
        if (PackageStates.DISABLED.equals(a2)) {
            return 3;
        }
        int packageVersionCode = packageManagerHelper.getPackageVersionCode(a.b());
        HMSLog.i("AvailableAdapter", "connect versionCode:" + packageVersionCode);
        if (packageVersionCode < this.a) {
            return 2;
        }
        return 0;
    }

    public boolean isUserResolvableError(int i) {
        switch (i) {
            case 1:
            case 2:
                return true;
            default:
                return false;
        }
    }

    public void startResolution(Activity activity, AvailableCallBack availableCallBack) {
        int i = 0;
        if (c.compareAndSet(false, true)) {
            HMSLog.i("AvailableAdapter", "Start to resolution for the 1st time.");
        } else if (Looper.getMainLooper() == Looper.myLooper()) {
            HMSLog.i("AvailableAdapter", "main thread invokes resolution.");
        } else {
            HMSLog.i("AvailableAdapter", "Another thread start to resolution.");
            while (c.get()) {
                if (i >= 300) {
                    HMSLog.i("AvailableAdapter", "Previous popup is not handled by user or download has not finished within 1min.");
                    if (availableCallBack != null) {
                        availableCallBack.onComplete(27);
                        return;
                    }
                    return;
                }
                try {
                    HMSLog.d("AvailableAdapter", "Concurrent startResolution thread is waiting.");
                    Thread.sleep(200);
                    i++;
                } catch (InterruptedException e) {
                    HMSLog.d("AvailableAdapter", "Concurrent startResolution thread waiting is interrupted.");
                }
            }
            HMSLog.i("AvailableAdapter", "Concurrent startResolution thread waiting finished.");
            if (availableCallBack != null) {
                availableCallBack.onComplete(d);
                return;
            }
            return;
        }
        HMSLog.i("AvailableAdapter", "startResolution");
        if (activity != null && availableCallBack != null) {
            this.b = availableCallBack;
            a.b().a(this.e);
            Intent intentStartBridgeActivity = BridgeActivity.getIntentStartBridgeActivity(activity, b.class.getName());
            intentStartBridgeActivity.putExtra(MapKey.UPDATE_VERSION, this.a);
            activity.startActivity(intentStartBridgeActivity);
        }
    }

    private AvailableCallBack c() {
        return this.b;
    }
}
